package dsk.demo.miaosha.demo.utils;

import com.alibaba.fastjson.JSON;
import dsk.demo.miaosha.demo.vo.RespBean;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.*;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class UserUtil {

    public static void main(String[] args) throws SQLException, IOException, ClassNotFoundException {
        UserUtil.HttpGetCookie();
    }

    public static void creatUser() throws IOException, SQLException, ClassNotFoundException {
        //1.提供java.sql.Driver接口实现类的对象
        String driver = "com.mysql.jdbc.Driver";
        Class.forName(driver);

        //2.提供url，指明具体操作的数据
        String url = "jdbc:mysql://localhost:3306/seckill?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai";

        //3.提供Properties的对象，指明用户名和密码
        Properties info = new Properties();
        info.setProperty("user", "root");
        info.setProperty("password", "1234");

        //4.调用driver的connect()，获取连接
        Connection conn = DriverManager.getConnection(url, info);

        for (int i=0;i<5000;i++){
            String sql = "insert into t_user (`id`,`nickname`,`password`,`salt`)  VALUES (?,?,?,?);";
            String userId = Long.toString(18000000000l + i);
            String nickName = "戴世坤" + i;
            String password = "1234";
            String salt = "1a2b3c4d";
            password = MD5Util.inputPassToDBPass(password,salt);
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,userId);
            pstmt.setString(2,nickName);
            pstmt.setString(3,password);
            pstmt.setString(4,salt);
            pstmt.execute();
            pstmt.close();
        }
        conn.close();

    }

    public static void HttpGetCookie() throws IOException {
        File file = new File("C:\\Users\\Administrator\\Desktop\\userInfo");
        Writer writer = new FileWriter(file);

        for (int i=0;i<5000;i++){
            List<NameValuePair> formparams = new ArrayList<NameValuePair>();
            String mobile = Long.toString(18000000000l + i);
            String password = "1234";
            formparams.add(new BasicNameValuePair("mobile", mobile));
            formparams.add(new BasicNameValuePair("password", MD5Util.inputPassFromPass("1234")));
            HttpEntity reqEntity = new UrlEncodedFormEntity(formparams, "utf-8");

            RequestConfig requestConfig = RequestConfig.custom()
                    .setConnectTimeout(5000)//一、连接超时：connectionTimeout-->指的是连接一个url的连接等待时间
                    .setSocketTimeout(5000)// 二、读取数据超时：SocketTimeout-->指的是连接上一个url，获取response的返回等待时间
                    .setConnectionRequestTimeout(5000)
                    .build();
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost post = new HttpPost("http://localhost:8080/login/doLogin");
            post.setEntity(reqEntity);
            post.setConfig(requestConfig);
            HttpResponse response = httpClient.execute(post);
            HttpEntity resEntity = response.getEntity();
            String message = EntityUtils.toString(resEntity, "utf-8");
            System.out.println(message);
            if (response.getStatusLine().getStatusCode() == 200) {
                RespBean respBean = JSON.parseObject(message, RespBean.class);
                String cookie = (String)respBean.getObj();
                System.out.println(cookie);
                writer.write(mobile+","+cookie+"\r\n");
            } else {
                System.out.println("请求失败");
            }
        }
    }
}
